package com.ruoyi.industryfrontend.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industryfrontend.domain.ProjectAuction;
import com.ruoyi.industryfrontend.domain.ProjectPark;
import com.ruoyi.industryfrontend.domain.SpaceIndustry;
import com.ruoyi.industryfrontend.mapper.*;
import com.ruoyi.industryfrontend.service.IProjectAuctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目-拍卖Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-18
 */
@Service
public class ProjectAuctionServiceImpl implements IProjectAuctionService
{
    @Autowired
    private ProjectAuctionMapper projectAuctionMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;

    /**
     * 查询项目-拍卖
     *
     * @param id 项目-拍卖主键
     * @return 项目-拍卖
     */
    @Override
    public ProjectAuction selectProjectAuctionById(Long id)
    {
        return projectAuctionMapper.selectProjectAuctionById(id);
    }

    @Override
    public Map<String, Object> selectProjectAuctionByNumber(String spaceId) {
        ProjectAuction projectAuction = projectAuctionMapper.selectProjectAuctionByNumber(spaceId);
        List<String> list = spaceCollectMapper.selectSpaceCollectByUserId(SecurityUtils.getUserId(),spaceId);
        if (list!=null&&list.size()!=0){
            projectAuction.setCollectState(1L);
        }else {
            projectAuction.setCollectState(0L);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("space",projectAuction);
        if (projectAuction.getParkId()!=null) {
            ProjectPark projectPark = projectParkMapper.selectProjectParkById(projectAuction.getParkId());
            map.put("park",projectPark);
        }
        return map;
    }

    /**
     * 查询项目-拍卖列表
     *
     * @param projectAuction 项目-拍卖
     * @return 项目-拍卖
     */
    @Override
    public List<ProjectAuction> selectProjectAuctionList(ProjectAuction projectAuction)
    {
//        List<Long> districtIds = new ArrayList<Long>();
//        //查询城市
//        if(projectAuction.getCityId()!=null){
//            districtIds.add(projectAuction.getCityId());
//            projectAuction.setCityIds(districtIds);
//        }else if (projectAuction.getCityId()==null&&projectAuction.getProvinceId()!=null){
//            //查询省份下的城市
//            districtIds.addAll(districtMapper.selectDistrictIdList(projectAuction.getProvinceId()));
//            districtIds.add(projectAuction.getProvinceId());
//            projectAuction.setCityIds(districtIds);
//        }
        projectAuction.setSupplyUserId(SecurityUtils.getUserId());
        return projectAuctionMapper.selectProjectAuctionListz(projectAuction);
    }

    /**
     * 新增项目-拍卖
     *
     * @param projectAuction 项目-拍卖
     * @return 结果
     */
    @Override
    public int insertProjectAuction(ProjectAuction projectAuction)
    {
        projectAuction.setSupplyUserId(SecurityUtils.getUserId());
        projectAuction.setApprovalState(1L);//审核状态
        projectAuction.setCreateTime(DateUtils.getNowDate());
        projectAuction.setSerialNumber(IdUtils.fastSimpleUUID());

        SpaceIndustry spaceIndustry = new SpaceIndustry();
        spaceIndustry.setSpaceId(projectAuction.getSerialNumber());
        spaceIndustry.setCreateBy(SecurityUtils.getUsername());
        spaceIndustry.setCreateTime(projectAuction.getCreateTime());
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        extracted(projectAuction, spaceIndustry);

        spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);
        return projectAuctionMapper.insertProjectAuction(projectAuction);
    }

    private static void extracted(ProjectAuction projectAuction, SpaceIndustry spaceIndustry) {
        spaceIndustry.setBuildType(7);
        spaceIndustry.setReleaseType(0L);
        spaceIndustry.setApprovalState(1L);
        spaceIndustry.setCityId(projectAuction.getCityId());
        spaceIndustry.setParkId(String.valueOf(projectAuction.getParkId()));
        spaceIndustry.setLeasePrice(projectAuction.getAuctionPrice());
        spaceIndustry.setSellPrice(projectAuction.getAuctionPrice());
        spaceIndustry.setCoverResource(projectAuction.getCoverResource());
        spaceIndustry.setSpaceName(projectAuction.getAuctionName());
    }

    /**
     * 修改项目-拍卖
     *
     * @param projectAuction 项目-拍卖
     * @return 结果
     */
    @Override
    public int updateProjectAuction(ProjectAuction projectAuction)
    {
        projectAuction.setUpdateTime(DateUtils.getNowDate());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectAuction,spaceIndustry);
        spaceIndustry.setSpaceId(String.valueOf(projectAuction.getId()));
        spaceIndustry.setUpdateBy(SecurityUtils.getUsername());
        spaceIndustry.setUpdateTime(projectAuction.getUpdateTime());
        spaceIndustryMapper.updateSpaceIndustryByAuctionId(spaceIndustry);
        return projectAuctionMapper.updateProjectAuction(projectAuction);
    }

    /**
     * 批量删除项目-拍卖
     *
     * @param ids 需要删除的项目-拍卖主键
     * @return 结果
     */
    @Override
    public int deleteProjectAuctionByIds(Long[] ids)
    {
        return projectAuctionMapper.deleteProjectAuctionByIds(ids);
    }

    /**
     * 删除项目-拍卖信息
     *
     * @param id 项目-拍卖主键
     * @return 结果
     */
    @Override
    public int deleteProjectAuctionById(Long id)
    {
        spaceIndustryMapper.deleteSpaceIndustryByProjectParkId(id);
        return projectAuctionMapper.deleteProjectAuctionById(id);
    }

    @Override
    public int deleteProjectAuctionBySerialNumber(String spaceId) {
        spaceIndustryMapper.deleteSpaceIndustryBySerialNumber(spaceId);
        return projectAuctionMapper.deleteProjectAuctionBySerialNumber(spaceId);
    }
}
